package biz.almas.uycnr.crawler.dao.factory;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

import cn.ffcs.memory.Memory;

/**
 * Memory 工厂类
 * @author Ansar786
 *
 */
public class MemoryFactory {
	
	//定义日志输出对象
    private static org.apache.log4j.Logger logger = 
    				org.apache.log4j.Logger.getLogger(MemoryFactory.class);
    
	private MemoryFactory() {

	}

	private static class SingletonHolder {
		//从配置文件里读取数据库信息		--》发布后的代码中同一用
//		public static final Memory MEMORY = new Memory(getDataSource(),true);
		//从代码里读取数据库信息
		public static final Memory MEMORY = new Memory(new SimpleDataSource());
	}

	public static Memory getInstance() {
		return SingletonHolder.MEMORY;
	}
	
	public static final DataSource getDataSource() {
		try {
			Context context = new InitialContext();
			return (DataSource) context.lookup("java:comp/env/jdbc/uycnr");
		} catch (NamingException e) {
			logger.error("无法连接数据库：", e);
			return null;
		}
	}
}
